Component({
    options: {
        addGlobalClass: true
    },
    properties: {

        start: {
            type: String,
            value: '',
            observer: function (newVal) {
                if (typeof newVal == 'string' && newVal.length > 0) {
                    var t = new Date(newVal);
                    if (!isNaN(t.getTime())) {
                        var y = t.getFullYear(),
                            m = t.getMonth(),
                            d = t.getDate();
                        this.setData({
                            startDate: [y, m, d],
                        });
                    }
                }
            },
        },
        end: {
            type: String,
            value: '',
            observer: function (newVal) {
                if (typeof newVal == 'string' && newVal.length > 0) {
                    var t = new Date(newVal);
                    if (!isNaN(t.getTime())) {
                        var y = t.getFullYear(),
                            m = t.getMonth(),
                            d = t.getDate();
                        this.setData({
                            endDate: [y, m, d],
                        });
                    }
                }
            },
        },
        value: {
            type: String,
            value: '',
            observer: function (newVal) {
                if (typeof newVal == 'string' && newVal.length > 0) {
                    var t = new Date(newVal);
                    if (!isNaN(t.getTime())) {
                        var y = t.getFullYear(),
                            m = t.getMonth(),
                            d = t.getDate();
                        this.setData({
                            valueDate: [y, m, d],
                            year: y,
                            month: m,
                        });
                    }
                }
            },
        },

        weight: {
            type: String,
            value: "0"
        },
        integral: {
            type: Number,
            value: 1
        },
        record_date:{
            type:Array,
            value:[{
                record_date:""
            }],
        }
    },
    data: {
        show: true,
        startDate: [],
        endDate: [],
        valueDate: [],
        months:[],
        year: new Date().getFullYear(),
        month: new Date().getMonth(),
        days: [],
        integral: 1,
        record_date:[{
            record_date:''
        }],
        date:'',
    },
    methods: {
        loadData: function () {
            var that = this,
                data = that.data,
                month = data.month+1,
                months = [];
            for (var i = 1; i <= 12; i++) {
                var current = false,
                    disabled = false;
                if (i == month) current = true;
                months.push({
                    month: i,
                    current: current,
                    disabled: disabled,
                });
            }
            console.log(months);
            that.setData({
                months: months,
            });
        },
        prevMonth: function () {
            var that = this,
                data = that.data,
                year = data.year,
                month = data.month,
                date = new Date(year, month - 1, 1);
            that.setData({
                    year: date.getFullYear(),
                    month: date.getMonth(),
                    date:date,
                },
                () => {
                    that.loadData();
                }
            );
        },
        nextMonth: function () {
            var that = this,
                data = that.data,
                year = data.year,
                month = data.month,
                date = new Date(year, month + 1, 1);
            that.setData({
                    year: date.getFullYear(),
                    month: date.getMonth(),
                    date:date,
                },
                () => {
                    that.loadData();
                }
            );
        },
        changeMonth: function (e) {
            var that = this,
                value = e.detail.value,
                date = new Date(value + '-01');
            that.setData({
                    year: date.getFullYear(),
                    month: date.getMonth(),
                    date:date,
                },
                () => {
                    that.loadData();
                }
            );
        },
        changeDate: function (e) {
            var that = this,
                data = that.data,
                year = data.year,
                month = data.month,
                months = data.months,
                index = e.currentTarget.dataset.i,
                value = {};
                console.log('month',month)
                console.log('months',months)
                console.log('index',index)
            if (months[index] && months[index].month > 0 && !months[index].disabled) {
                if (!months[index].current) {
                    for (var i = 0, l = months.length; i < l; i++) {
                        if (months[i].current) {
                            months[i].current = false;
                            break;
                        }
                    }
                }
                months[index].current = true;
                that.setData({
                    months: months,

                });
                // that.closeCalendar();
                value.year = year;
                month =index+1;
                value.month = month;
                value.date = year + '-' + (month < 10 ? '0' + month : month) ;
                console.log(value)
                that.triggerEvent('doChange', value);
            }
        },
    },
    attached: function () {
        this.loadData();
    },

    
});